package com.rzcode.coinex.kernel.service;

import com.querydsl.core.types.Predicate;
import com.rzcode.coinex.core.constant.PageModel;
import com.rzcode.coinex.core.dto.MemberWalletDTO;
import com.rzcode.coinex.core.exception.InformationExpiredException;
import com.rzcode.coinex.core.util.MessageResult;
import com.rzcode.coinex.kernel.common.response.R;
import com.rzcode.coinex.kernel.dao.entity.Coin;
import com.rzcode.coinex.kernel.dao.entity.Member;
import com.rzcode.coinex.kernel.dao.entity.MemberWallet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

public interface MemberWalletService {

    /**
     * @param wallet
     * @return
     */
    MemberWallet save(MemberWallet wallet);

    /**
     * 获取钱包
     *
     * @param coin     otc币种
     * @param memberId
     * @return
     */
    MemberWallet findByOtcCoinAndMemberId(OtcCoin coin, long memberId);

    /**
     * 钱包充值
     *
     * @param wallet
     * @param amount
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    R recharge(MemberWallet wallet, BigDecimal amount);

    /**
     * 钱包充值
     *
     * @param coin    币种名称
     * @param address 地址
     * @param amount  金额
     * @return
     */
    R recharge(Coin coin, String address, BigDecimal amount, String txid);


    /**
     * 钱包充值(EOS地址类型）
     *
     * @param coin    币种名称
     * @param address 地址
     * @param amount  金额
     * @return
     */
    R recharge2(Long userId, Coin coin, String address, BigDecimal amount, String txid);

    /**
     * 根据币种和钱包地址获取钱包
     *
     * @param coin
     * @param address
     * @return
     */
    MemberWallet findByCoinAndAddress(Coin coin, String address);

    /**
     * 根据币种和用户ID获取钱包
     *
     * @param coin
     * @param member
     * @return
     */
    MemberWallet findByCoinAndMember(Coin coin, Member member);

    MemberWallet findByCoinUnitAndMemberId(String coinUnit, Long memberId);

    MemberWallet findByCoinAndMemberId(Coin coin, Long memberId);

    /**
     * 根据用户查找所有钱包
     *
     * @param member
     * @return
     */
    List<MemberWallet> findAllByMemberId(Member member);

    List<MemberWallet> findAllByMemberId(Long memberId);

    /**
     * 冻结钱包
     *
     * @param memberWallet
     * @param amount
     * @return
     */
    MessageResult freezeBalance(MemberWallet memberWallet, BigDecimal amount);

    /**
     * 解冻钱包
     *
     * @param memberWallet
     * @param amount
     * @return
     */
    R thawBalance(MemberWallet memberWallet, BigDecimal amount);

    /**
     * 放行更改双方钱包余额
     *
     * @param order
     * @param ret
     * @throws InformationExpiredException
     */
    void transfer(Order order, int ret) throws InformationExpiredException;


    /**
     * 放行更改双方钱包余额
     */
    void transferAdmin(Order order, int ret) throws InformationExpiredException;

    void trancerDetail(Order order, long sellerId, long buyerId) throws InformationExpiredException;

    int deductBalance(MemberWallet memberWallet, BigDecimal amount);

    List<MemberWallet> findAll() {
        return memberWalletDao.findAll();
    }

    List<MemberWallet> findAllByCoin(Coin coin) {
        return memberWalletDao.findAllByCoin(coin);
    }

    /**
     * 锁定钱包
     *
     * @param uid
     * @param unit
     * @return
     */
    boolean lockWallet(Long uid, String unit);

    /**
     * 解锁钱包
     *
     * @param uid
     * @param unit
     * @return
     */
    boolean unlockWallet(Long uid, String unit);

    MemberWallet findOneByCoinNameAndMemberId(String coinName, long memberId);

    Page<MemberWalletDTO> joinFind(List<Predicate> predicates, QMember qMember, QMemberWallet qMemberWallet, PageModel pageModel);

    BigDecimal getAllBalance(String coinName);

    MemberDeposit findDeposit(String address, String txid);

    MemberDeposit findDepositByTxid(String txid);

    void decreaseFrozen(Long walletId, BigDecimal amount);

    void increaseBalance(Long walletId, BigDecimal amount);

    int unfreezeLess();

    int unfreezeMore();

    int dropWeekTable(int weekDay);

    int createWeekTable(int weekDay);

    BigDecimal getWalletBalanceAmount(String coinId, int week);

    List<MemberWallet> geteveryBHB(String coinName, int week);


    //更新团队钱包
    int updateTeamWallet(BigDecimal teamBalance, long teamId);

    /**
     * 根据 coinId和会员ID查询会员账户信息
     *
     * @param coinId
     * @param memberId
     * @return
     */
    MemberWallet getMemberWalletByCoinAndMemberId(String coinId, long memberId);

    /**
     * 根据用户ID和coinID更新用户钱包
     */
    @Transactional
    int updateByMemberIdAndCoinId(long memberId, String coinId, BigDecimal balance);

    /**
     * 增加用户BHB钱包余额
     *
     * @param mineAmount
     * @param memberId
     * @return
     */
    int increaseBalanceForBHB(BigDecimal mineAmount, Long memberId);

    /**
     * 查询待释放BHB大于500的
     */
    List<MemberWallet> findUnfreezeGTE();

    /**
     * 查询待释放BHB大于500的
     */
    List<MemberWallet> findUnfreezeLTE();


    int updateBalanceByIdAndAmount(long id, double amount);

    /**
     * 增加冻结资产（与余额无关）
     *
     * @param id
     * @param amount
     */
    void increaseFrozen(Long id, BigDecimal amount);

    /**
     * 增加待释放资产（与余额无关）
     *
     * @param id
     * @param amount
     */
    void increaseToRelease(Long id, BigDecimal amount);

    /**
     * 减少待释放资产（与余额无关）
     *
     * @param id
     * @param amount
     */
    void decreaseToRelease(Long id, BigDecimal amount);

}
